﻿var paginationTotal = 20;//记录总数
var isSearch = false;
function submitsearch() {
    isSearch = true;
    paginationCurrentPage = 1;
    initData(0);
}

function loadData(page, pageSize) {
    var vForm = "";
    if (isSearch == true && isSearch != undefined) {
        vForm = $("#sForm").serializeArray();
    }
    loading();
    $.ajax({
        type: "POST",
        async: false,
        dataType: "json",
        url: "/warehouse/house/list/page/" + page + "/pageSize/" + pageSize,
        data: vForm,
        success: function(json) {
            closeLoading();
            var list = "";
            paginationTotal = json.count;
            if (json.ask == 0) {
                list = "<td colspan='7' class=\"center\">&nbsp;<{$lang.no_data}></td>";
            } else {
                $.each(json.result, function(key, val) {
                    var statusIcon = val.wh_status == 0 ? "unnormal.png" : "normal.png";
                    if ((key + 1) % 2 == 1) {
                        list += "<tr class=''>";
                    } else {
                        list += "<tr class='even-tr'>";
                    }
                    list += "<td>" + val.wh_code + "</td>";
                    list += "<td>" + val.warehouse_name + "</td>";
                    list += "<td>" + val.wa_code + "</td>";
                    list += "<td>" + val.wh_note + "</td>";
                    list += "<td>" + val.wh_valuable + "</td>";
                    list += "<td>" + val.wh_create_date + "</td>";
                    list += "<td class=\"center\"><img src=\"/images/icon/" + statusIcon + "\"></td>";
                    list += "<td class=\"center\"><a href=\"javascript:edit(" + val.wh_id + ")\"><img src=\"/images/icon/edit.gif\"></a>&nbsp;&nbsp;|&nbsp;&nbsp;<a href=\"javascript:delelteInfo(" + val.wh_id + ")\"><img src=\"/images/icon/delete.gif\"></a></td>";
                    list += "</tr>";
                });
            }
            $("#loadListData").html(list);
        }
    });
}

//<{$lang.warehouse}><{$lang.list}>
function loadSelect() {
    $.ajax({
        type: "post",
        async: false,
        dataType: "json",
        url: "/warehouse/house/select",
        data: null,
        success: function(json) {
            var list = "";
            if (json.ask == 1) {
                $.each(json.result, function(key, val) {
                    list += "<option value=" + val.warehouse_id + ">" + val.warehouse_name + "</option>";
                });
                $("#warehouse_id").html(list);
            }
        }
    });
}

//<{$lang.area}><{$lang.list}>
function loadAreaSelect() {
    $.ajax({
        type: "post",
        async: false,
        dataType: "json",
        url: "/warehouse/area/select",
        data: null,
        success: function(json) {
            var list = "";
            if (json.ask == 1) {
                $.each(json.result, function(key, val) {
                    list += "<option value=" + val.wa_code + ">" + val.wa_name + "</option>";
                });
                $("#wa_code").html(list);
            }
        }
    });
}

//<{$lang.delete}>
function delelteInfo(id) {
    if (id == '' || id == undefined) {
        return false;
    }
    Uid = id;
    $('#right_confirm').dialog('open');
}

function dialogclose() {
    $("#dialog").dialog("close");
}

$(function() {
    initData(0);
    $("#dialog").dialog({
        autoOpen:false,
        modal:true,
        width:380,
        show:"slide",
        buttons: {
            '<{$lang.ok}>': function() {
                create();
            },
            '<{$lang.cancel}>': function() {
                $(this).dialog('close');
            }
        },
        close: function() {
            tip('');//<{$lang.clear}><{$lang.tip}><{$lang.msg}>
            $('#createForm')[0].reset();
        }
    });

    $("#right_confirm").dialog({
        autoOpen:false,
        modal:true,
        height:160,
        show:"slide",
        buttons: {
            '<{$lang.ok}>': function() {
                $.ajax({
                    type: "post",
                    async: false,
                    dataType: "json",
                    url: "/warehouse/house/delete",
                    data: "wh_id=" + Uid,
                    success: function(json) {
                        if (json.ask == '1') {
                            paginationTotal = paginationTotal - 1;
                            initData(paginationCurrentPage - 1);
                            //changePageSize($("#pageSize"));
                            messags = "<{$lang.success}><{$lang.delete}>！";
                        } else {
                            messags = "<{$lang.delete}><{$lang.fail}>！";
                        }
                        alertTip(messags);
                    }
                });
                $(this).dialog('close');
            },
            '<{$lang.cancel}>': function() {
                $(this).dialog('close');
            }
        }

    });
    //<{$lang.add}>
    $("#create").click(function() {
        tip('');
        $("#dialog").dialog({title:'<{$lang.add}><{$lang.room}>',autoOpen:true});
        loadSelect();//<{$lang.warehouse}>
        loadAreaSelect();//<{$lang.area}>
        tip('');//<{$lang.clear}><{$lang.tip}><{$lang.msg}>
        $('#div_wh_code').html('<input type="text" name="wh_code" id="wh_code" class="text ui-widget-content"/>');
    });
    $("#close").click(function() {
        dialogclose();
    });


    function create() {
        vForm = "";
        vForm = $("#createForm").serializeArray();
        $.ajax({
            type: "post",
            async: false,
            dataType: "json",
            url: "/warehouse/house/create",
            data: vForm,
            success: function(json) {
                if (json.ask != '0') {
                    if (json.ask == 1) {
                        alertTip('<{$lang.add}><{$lang.success}>');
                        initData(0);
                        dialogclose();
                    } else if (json.ask == 2) {
                        alertTip('<{$lang.edit}><{$lang.success}>');
                        loadData(paginationCurrentPage, paginationPageSize);
                        dialogclose();
                    } else if (json.ask == 4) {
                        tip("" + $("[name=wh_code]").val() + "<{$lang.exists}>！");
                    } else if (json.ask == 5) {
                        tip("<{$lang.warehouse}><{$lang.area}><{$lang.code}>" + $("[name=wa_code]").val() + "<{$lang.not_exists}>！");
                    } else {
                        tip("<{$lang.edit}><{$lang.data}>?");
                    }
                } else {
                    if (json.error == null)return;
                    var html = '';
                    $.each(json.error, function(key, val) {
                        html += '<span><img src="/images/no.gif">' + val + '</span>';
                    })
                    tip(html);
                }
            }
        });
    }
});

function tip(mess) {
    $("#tips").html(mess);
    if (mess != '') {
        $("#tips").show();
    } else {
        $("#tips").hide();
    }
}

function edit(id) {
    tip('');//<{$lang.clear}><{$lang.tip}><{$lang.msg}>
    loadSelect();//<{$lang.warehouse}>
    loadAreaSelect();//<{$lang.area}>
    $.ajax({
        type: "post",
        async: false,
        dataType: "json",
        url: "/warehouse/house/info",
        data: "wh_id=" + id,
        success: function(json) {
            if (json.ask == 1) {
                $('#div_wh_code').html(json.result.wh_code+"<input type=\"hidden\" value=\""+json.result.wh_code+"\" name=\"wh_code\" id=\"wh_code\" />");
                $("[name=wa_code]").val(json.result.wa_code);
                $("[name=warehouse_id]").val(json.result.warehouse_id);
                $("[name=wh_note]").val(json.result.wh_note);
                $("[name=wh_valuable]").val(json.result.wh_valuable);
                $("[name=wh_status]").val(json.result.wh_status);
                $("[name=wh_id]").val(json.result.wh_id);
                $("#dialog").dialog({title:'<{$lang.edit}><{$lang.room}>',autoOpen:true});
            }
        }
    });
}
//<{$lang.operation}><{$lang.tip}>
function alertTip(tip) {
    $('<div title="<{$lang.tip}>"><p align="center">' + tip + '</p></div>').dialog({
        modal: true,
        buttons: {
            '<{$lang.ok}>': function() {
                $(this).dialog("close");
            }
        }
    });
}